import 'package:fish_redux/fish_redux.dart';
import 'package:school_safe_teacher/constans/constants.dart';
import 'package:school_safe_teacher/homeWorkList/entity/homeWorkListEntity.dart';

import 'action.dart';
import 'state.dart';

Reducer<homeWorkListState> buildReducer() {
  return asReducer(
    <Object, Reducer<homeWorkListState>>{
      homeWorkListAction.action: _onAction,
      homeWorkListAction.onSuccess: _onSuccess,
    },
  );
}

homeWorkListState _onAction(homeWorkListState state, Action action) {
  final homeWorkListState newState = state.clone();
  return newState;
}


homeWorkListState _onSuccess(homeWorkListState state, Action action) {
  final homeWorkListState newState = state.clone();
  List<homeWorkListEntity> list = action.payload;
  if (list.length < Constants.PAGE_SIZE) {
    state.easyRefreshController.finishLoad(noMore: true);
  }
  if (newState.page == 1) {
    newState..item = list;
    state.easyRefreshController.finishRefresh();
  } else {
    newState.item.addAll(list);
    state.easyRefreshController.finishLoad(noMore:list.length!=Constants.PAGE_SIZE);
  }
  return newState;
}